//引入angular的infinite-scroll模块
angular.module('myApp', ['infinite-scroll']);

//控制图片大小
var max = document.documentElement.clientWidth;

function initImg(listimg) {
	listimg.style.width = (max - 60) / 3.0 + "px";
	listimg.style.height = (max - 60) / 3.0 + "px";
}

function searchCircle($scope, $http) {
	var SearchUrl = getUrl("circleSearch");
	var cname = '';
	var historys = [];
	var phoneId;
	if (localStorage.historys) {
		if (localStorage.account) {
			phoneId = readData(localStorage.phone)
		}
		var localhistoryObj = JSON.parse(localStorage.historys);
		if (localhistoryObj[phoneId]) {
			$scope.historys = localhistoryObj[phoneId];
		} else {
			$scope.historys = historys;
		}
	} else {
		$scope.historys = historys;
	}
	//总页数初始默认0，读取数据后再更新
	var flag = true;
	var searchval = document.getElementById("search");
	$scope.search = function() {
		var val = searchval.value.replace(/^ +| +$/g, "");
		if (val.length <= 0) //控制搜索长度不能
			return;
		if (localStorage.historys) {
			var localhistoryObj = JSON.parse(localStorage.historys);
			if (localhistoryObj[phoneId]) {
				historys = localhistoryObj[phoneId];
				//判断搜索是否存在
				for (var i = 0; i < historys.length; i++) {
					if (historys[i] == $scope.circleName) {
						flag = false;
					}
				}
				if (flag) {
					if (historys.length > 9) {
						historys.shift();
						historys.push($scope.circleName);
						var historyObj = localhistoryObj;
						historyObj[phoneId] = historys;
						localStorage.historys = JSON.stringify(historyObj);
					} else {
						historys.push($scope.circleName);
						var historyObj = localhistoryObj;
						historyObj[phoneId] = historys;
						localStorage.historys = JSON.stringify(historyObj);
					}
				}
			} else {
				historys.push($scope.circleName);
				var historyObj = localhistoryObj;
				historyObj[phoneId] = historys;
				localStorage.historys = JSON.stringify(historyObj);
			}
		} else {
			historys.push($scope.circleName);
			var historyObj = {};
			historyObj[phoneId] = historys;
			localStorage.historys = JSON.stringify(historyObj);
		}
		flag = true;
		$scope.historys = historys;
		$scope.pages = 0;
		//默认加载第一页，初始值0
		$scope.Circles = [];
		$scope.totalpage = 0;
		cname = $scope.circleName;
		$scope.circleName = "";	
		if (cname != "" && cname != null) {
			$scope.pages = 0;
			$http({
				method: 'POST',
				url: SearchUrl,
				timeout: 10000,
				//cache:true,
				params: {
					indexpage: $scope.pages,
					pageSize: 8,
					name: cname
				}
			}).success(function(data, status, headers, config) {
				if (data.obj) {
					$scope.Circles = data.obj.list;
					$scope.totalpage = data.obj.totalpage;
					for (var i = 0; i < $scope.Circles.length; i++) {
						for (var j = 0; j < $scope.Circles[i].productList.length; j++) {
							if (!$scope.Circles[i].productList[j].picture) {
								$scope.Circles[i].productList[j].picture = '../../image/small.png';
							}
						}
					}
					//下拉页面+1
					$scope.pages = $scope.pages + 1;
					document.getElementById("historys").style.display = "none";
					document.getElementById("adv").style.display = "none";
				} else {
					$scope.Circles = [];
					mui.toast('您查找的圈子不存在~谢谢');				
				}
			}).error(function() {		
				mui.alert('网络连接异常，请稍后重试(e) ', '提示', function() {
					//info.innerText = '你刚关闭了警告框';
				});
			})
		}
	}
	$scope.loadMore = function() {
			if ($scope.pages < $scope.totalpage) {
				$scope.pages = $scope.pages + 1;
				$http({
					method: 'POST',
					url: SearchUrl,
					timeout: 10000,
					//cache:true,
					params: {
						indexpage: $scope.pages,
						pageSize: 5,
						name: cname
					}
				}).success(function(data, status, headers, config) {
					if (data.obj) {
						var circle = data.obj.list;
						for (var i = 0; i < circle.length; i++) {
							$scope.Circles.push(circle[i])
						}
						for (var i = 0; i < $scope.Circles.length; i++) {
							for (var j = 0; j < $scope.Circles[i].productList.length; j++) {
								if (!$scope.Circles[i].productList[j].picture) {
									$scope.Circles[i].productList[j].picture = '../../image/small.png';
								}
							}
						}
						$scope.totalpage = data.obj.totalpage;
						//下拉页面+1
					} else {
						$scope.Circles = [];
						$scope.pages = $scope.pages - 1;
					}
				}).error(function() {
					mui.alert('网络连接异常，请稍后重试(e) ', '提示', function() {
						//info.innerText = '你刚关闭了警告框';
					});
					$scope.pages = $scope.pages - 1;
				})
			}
		}
		//读取推荐圈子
	var urlMore = getUrl("57noCircle");
	if (localStorage.currentCity && localStorage.currentCity != "") {
		params1 = {
			"indexpage": 0,
			"pageSize": 10,
			"city": readData(localStorage.currentCity)
		}
	} else {
		params1 = {
			"indexpage": 0,
			"pageSize": 10
		}
	}
	$scope.pCircles = [];
	$http({
		method: 'POST',
		url: urlMore,
		timeout: 10000,
		params: params1
	}).success(function(data, status, headers, config) {
		if (data.obj) {
			var circle = data.obj.list;
			for (var i = 0; i < circle.length; i++) {
				$scope.pCircles.push(circle[i])
			}
		} else {
			$scope.pCircles = []
		}
	}).error(function() {
		//alert('网络链接异常，请稍后重试(e)')
	});
	$scope.guanzhu = function(gid, groupId) {
		//当前圈子信息
		localStorage.guideId = writeData(gid);
		localStorage.youHId = writeData(groupId);
		mui.openWindow({
			url: 'guanzhu.html',
			id: 'guanzhu'
		});
	}
	$scope.addSearch = function(str) {
		$scope.circleName = str;
	}
}